/** 处理两点之间的短线问题, 输入为矩阵 index*/
export function pathFill(xA: f32, yA: f32, xB: f32, yB: f32): f32[][] {
    const pointB = [xB, yB];
    const isNeighbor = isNeigh(xA, yA, xB, yB);
    if (isNeighbor) {
        return [pointB];
    } else {
        // 具体方式是使用两点式方程求出两点间的连线，然后填充上去
        const line = [] as Array<f32[]>;
        const diff = [xA - xB, yA - yB];
        const axisName = Math.abs(diff[0]) >= Math.abs(diff[1]) ? 0 : 1;
        const symbol = diff[axisName] > 0 ? 1 : -1;
        let k = (yA - yB) / (xA - xB);
        const maxK = f32(1000);
        const b = yA - k * xA;
        const edge = Math.abs(diff[axisName]);
        // 根据长轴进行遍历
        for (let axis = 0; axis <= edge; axis++) {
            let trueX = f32(0),
                trueY = f32(0);
            const target = pointB[axisName] + f32(axis * symbol);
            if (axisName === 0) {
                trueX = target;
                trueY = k * trueX + b;
            } else {
                trueY = target;
                // 在竖直的状态时会导致 k 值无限，所以 k 大于一定量时直接赋值为 xB
                trueX = k > maxK || k < -maxK ? xB : (trueY - b) / k;
            }
            line.push([trueX, trueY]);
        }
        line.push(pointB);
        return line;
    }
}
function isNeigh(xA: f32, yA: f32, xB: f32, yB: f32): bool {
    const around = [
        [xA, yA],
        [xA - 1, yA],
        [xA + 1, yA],
        [xA, yA - 1],
        [xA, yA + 1],
        [xA - 1, yA - 1],
        [xA + 1, yA + 1],
        [xA + 1, yA - 1],
        [xA - 1, yA + 1],
    ];
    let isNeighbor = false;
    for (let index = 0; index < around.length; index++) {
        const it = around[index];

        if (it[0] === xB && it[1] === yB) {
            isNeighbor = true;
            break;
        }
    }

    return isNeighbor;
}
